﻿using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.Windows.Forms;

namespace ToolUtil
{
    public class ImgTool
    {
        public static Bitmap Cut(Bitmap b,PictureBox picbox, int StartX, int StartY, int iWidth, int iHeight)
        {
            if (b == null)
            {
                return null;
            }
            var wfactor = (double)b.Width / picbox.ClientSize.Width;
            var hfactor = (double)b.Height / picbox.ClientSize.Height;
            var resizeFactor = Math.Max(wfactor, hfactor);
            int w = (int)(b.Width / resizeFactor);
            int h = (int)(b.Height / resizeFactor);
            if (StartX >= w || StartY >= h)
            {
                return null;
            }
            if (StartX + iWidth > w)
            {
                iWidth = w - StartX;
            }
            if (StartY + iHeight > h)
            {
                iHeight = h - StartY;
            }
            try
            {
                Bitmap bmpOut = new Bitmap(iWidth, iHeight, PixelFormat.Format24bppRgb);
                Graphics g = Graphics.FromImage(bmpOut);
                g.DrawImage(b, new Rectangle(0, 0, iWidth, iHeight), new Rectangle(StartX, StartY, iWidth, iHeight), GraphicsUnit.Pixel);
                g.Dispose();
                bmpOut.Save(@"D:\11.jpg", ImageFormat.Jpeg);
                Console.WriteLine(w + "::" + h + "::" + bmpOut.Width + "::" + bmpOut.Height+"::"+ iWidth+"::"+ iHeight);
                return bmpOut;
            }
            catch
            {
                return null;
            }
        }
        public static Rectangle? getImageRegion(PictureBox pic)
        {
            if (pic.Image == null)
                return null;

            var picHeight = pic.Width * pic.Image.Height / pic.Image.Width;
            if (picHeight > pic.Height)
            {
                var imageHeight = pic.Height;
                var imageWidth = imageHeight * pic.Image.Width / pic.Image.Height;
                return new Rectangle((pic.Width - imageWidth) / 2, (pic.Height - imageHeight) / 2, imageWidth, imageHeight);
            }
            else
            {
                var imageWidth = pic.Width;
                var imageHeight = imageWidth * pic.Image.Height / pic.Image.Width;
                return new Rectangle((pic.Width - imageWidth) / 2, (pic.Height - imageHeight) / 2, imageWidth, imageHeight);
            }
        }
        /// <summary>
        /// 剪裁 -- 用GDI+
        /// </summary>
        /// <param name="b">原始Bitmap</param>
        /// <param name="StartX">开始坐标X</param>
        /// <param name="StartY">开始坐标Y</param>
        /// <param name="iWidth">宽度</param>
        /// <param name="iHeight">高度</param>
        /// <returns>剪裁后的Bitmap</returns>
        public static Bitmap Cut(Bitmap b, int StartX, int StartY, int iWidth, int iHeight)
        {
            if (b == null)
            {
                return null;
            }
            int w = b.Width;
            int h = b.Height;
            if (StartX >= w || StartY >= h)
            {
                return null;
            }
            if (StartX + iWidth > w)
            {
                iWidth = w - StartX;
            }
            if (StartY + iHeight > h)
            {
                iHeight = h - StartY;
            }
            try
            {
                Bitmap bmpOut = new Bitmap(iWidth, iHeight, PixelFormat.Format24bppRgb);
                Graphics g = Graphics.FromImage(bmpOut);
                g.DrawImage(b, new Rectangle(0, 0, iWidth, iHeight), new Rectangle(StartX+1, StartY+1, iWidth-1, iHeight-1), GraphicsUnit.Pixel);
                g.Dispose();
                //bmpOut.Save(@"D:\11.jpg", ImageFormat.Jpeg);
                //Console.WriteLine(w+"::"+h+"::"+bmpOut.Width + "::" + bmpOut.Height);
                return bmpOut;
            }
            catch
            {
                return null;
            }
        }
        public static Image Crop(Image image, Rectangle selection)
        {
            Bitmap bmp = image as Bitmap;
            // Check if it is a bitmap: 
            if (bmp == null)
                throw new ArgumentException("Kein gültiges Bild (Bitmap)");
            // Crop the image: 
            Bitmap cropBmp = bmp.Clone(selection, bmp.PixelFormat);
            // Release the resources: 
            cropBmp.Save(@"D:\12.jpg", ImageFormat.Jpeg);
            image.Dispose();
            return cropBmp;
        }
        public static Image Fit2PictureBox(Image image, PictureBox picBox)
        {
            Bitmap bmp = null;
            Graphics g;
            // Scale: 
            double scaleY = (double)image.Width / picBox.Width;
            double scaleX = (double)image.Height / picBox.Height;
            double scale = scaleY < scaleX ? scaleX : scaleY;

            // Create new bitmap: 
            bmp = new Bitmap(
             (int)((double)image.Width / scale),
             (int)((double)image.Height / scale));

            // Set resolution of the new image: 
            bmp.SetResolution(
             image.HorizontalResolution,
             image.VerticalResolution);

            // Create graphics: 
            g = Graphics.FromImage(bmp);

            // Set interpolation mode: 
            g.InterpolationMode = InterpolationMode.HighQualityBicubic;

            // Draw the new image: 
            g.DrawImage(
             image,
             new Rectangle(   // Ziel 
              0, 0,
              bmp.Width, bmp.Height),
             new Rectangle(   // Quelle 
              0, 0,
              image.Width, image.Height),
             GraphicsUnit.Pixel);

            // Release the resources of the graphics: 
            g.Dispose();
            bmp.Save(@"D:\13.jpg", ImageFormat.Jpeg);
            // Release the resources of the origin image: 
            image.Dispose();
            return bmp;
        }
    }
}
